/*
 * Copyright (C) 2008 Chris Bellew
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#import "iDynDNSd.h";

//iDynDNSd *_daemon;
int timerInterval;
BOOL _failedLastUpdate = NO;

extern UIApplication *theApp;

@implementation iDynDNSd


- (void) applicationDidFinishLaunching: (id) unused
{
	theApp = self;
	[self addStatusBarImageNamed:@"iDynDNS" removeOnAbnormalExit:YES];
	[[iDynDNSSettings sharedInstance] getCurrentIP];
	[[iDynDNSSettings sharedInstance] loadSettings];
	timerInterval = [[[iDynDNSSettings sharedInstance] updateInterval] intValue];
	[self tick:nil];
}

- (void) tick: (NSTimer *)unused
{
	[[iDynDNSSettings sharedInstance] getCurrentIP];
	
	// If the last update failed, only do another update if one of the settings has changed
	NSString *oldUsername = [[iDynDNSSettings sharedInstance] username];
	NSString *oldPassword = [[iDynDNSSettings sharedInstance] password];
	NSString *oldHostname = [[iDynDNSSettings sharedInstance] hostname];
	[oldUsername retain];
	[oldPassword retain];
	[oldHostname retain];
	[[iDynDNSSettings sharedInstance] loadSettings]; // Load settings in case the timer interval changed
	if(!_failedLastUpdate || ![oldUsername isEqualToString:[[iDynDNSSettings sharedInstance] username]] || ![oldUsername isEqualToString:[[iDynDNSSettings sharedInstance] username]] || ![oldUsername isEqualToString:[[iDynDNSSettings sharedInstance] username]]) {
	
		if([[[iDynDNSSettings sharedInstance] updateInterval] intValue] != timerInterval) {
			NSLog(@"updateInterval changed.");
			// Interval has changed since the daemon started.
			timerInterval = [[[iDynDNSSettings sharedInstance] updateInterval] intValue];
			//[self restart];
		}
		if([[[iDynDNSSettings sharedInstance] localAddress] isEqualToString:[[iDynDNSSettings sharedInstance] lastip]] || [[iDynDNSSettings sharedInstance] localAddress] == nil) {
			NSLog(@"IP hasn't changed. Sleeping.");
			_failedLastUpdate = NO;
		} else {
			NSLog(@"ip changed!");
			NSString *statusCode = [[iDynDNSSettings sharedInstance] updateIP];
			if([statusCode isEqualToString:@"good"]) {
				_failedLastUpdate = NO;
			} else {
				_failedLastUpdate = YES;
			}
		}
	} else {
		NSLog(@"Last update failed, and settings haven't changed. Skipping update.");
	}
	[oldUsername release];
	[oldPassword release];
	[oldHostname release];
	timer = [NSTimer scheduledTimerWithTimeInterval: timerInterval*60
                target: self
                selector: @selector(tick:)
                userInfo: nil
                repeats: NO];
}
@end

